package com.example.famhealing.timeline;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.example.famhealing.R;

public class ScrapTimelineActivity extends Activity implements OnClickListener{

	private ArrayList<ScrapList> array;
	private ScrapList data;
	private ScrapListAdapter adapter;
	ListView scraplist;

	String scrapUrl = "http://192.168.0.61:8080/famhealing/getFamScrap.get?phone_num=01011111111";

	String f_id = "";
	String scr_id = "";
	String user_id = "";
	String scr_pic_title = "";
	String scr_pic_body = "";
	String title = "";
	String url = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scrap_timeline);

		array = getScrap();

		scraplist = (ListView)findViewById(R.id.scrap_list);
		adapter = new ScrapListAdapter(this, android.R.layout.simple_list_item_1, array);
		scraplist.setAdapter(adapter);
		
		ImageButton conditionButton=(ImageButton)findViewById(R.id.timeline_condition);
		ImageButton healingButton=(ImageButton)findViewById(R.id.timeline_healing);
		conditionButton.setOnClickListener(this);
		healingButton.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.scrap_timeline, menu);
		return true;
	}
	
	public ArrayList<ScrapList> getScrap()
	{
		ArrayList<ScrapList> scraps=new ArrayList<ScrapList>();
		
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());

		try
		{
			URL url1=new URL(scrapUrl);
			InputStream in=url1.openStream();

			XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
			XmlPullParser parser=factory.newPullParser();
			
			parser.setInput(in, "euc-kr");
			
			int eventType=parser.getEventType();
			boolean isItemTag=false;
			String tagName="";
			boolean check=false;
			
			while(eventType!=XmlPullParser.END_DOCUMENT)
			{
				if(eventType==XmlPullParser.START_TAG)
				{
					tagName=parser.getName();
					if(!tagName.equals("data") && !tagName.equals("scrap"))
						isItemTag=true;
				}
				else if(eventType==XmlPullParser.TEXT && isItemTag==true)
				{
					if(tagName.equals("f_id"))
					{
						f_id=parser.getText();
					}
					if(tagName.equals("scr_id"))
					{
						scr_id=parser.getText();
					}
					if(tagName.equals("user_id"))
					{
						user_id=parser.getText();
					}
					if(tagName.equals("scr_pic_title"))
					{
						scr_pic_title=parser.getText();
					}
					if(tagName.equals("scr_pic_body"))
					{
						scr_pic_body=parser.getText();
					}
					if(tagName.equals("title"))
					{
						title=parser.getText();
					}
					if(tagName.equals("url"))
					{
						url=parser.getText();
					}
				}
				else if(eventType==XmlPullParser.END_TAG)
				{
					isItemTag=false;
					if(tagName.equals("url") && check==false)
					{
						data = new ScrapList("", "", "", "", "", title, url);
						scraps.add(data);
						check=true;
					}
					else if(!tagName.equals("url") && check==true)
						check=false;
				}
				
				eventType=parser.next();
			}
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		
		return scraps;
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch(v.getId())
		{
		case R.id.timeline_condition:
			intent=new Intent(getBaseContext(), TimelineActivity.class);
			startActivity(intent);
			finish();
			overridePendingTransition(0,0);
			break;
		case R.id.timeline_healing:
			intent=new Intent(getBaseContext(), HealingTimelineActivity.class);
			startActivity(intent);
			finish();
			overridePendingTransition(0,0);
			break;
		}
	}

}
